//
//  ABSDiscoverViewController.m
//  ABSallRec
//
//  Created by 我演示 on 2019/10/23.
//  Copyright © 2019 Jerry-Fu. All rights reserved.
//

#import "ABSDiscoverViewController.h"
#import "CategoryListLeftCell.h"

static NSString *const headerViewIndefine = @"CaseCluessKeybordCollectionHeadView";
static NSString *const footerViewIndefine = @"CaseCluessKeybordCollectionFootView";

static NSString *const CollectionViewCellId = @"Capital_HeaderCollectionViewCellId";



@interface ABSDiscoverViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource , UICollectionViewDelegateFlowLayout>

/**dataSource*/
@property(nonatomic,strong) NSMutableArray *dataSource;

/**collectionViewData*/
@property(nonatomic,strong) NSMutableArray *dataCollectionData ;
/**tableView*/
@property(nonatomic,strong) UITableView *leftTableView ;

/**collectionView*/
@property(nonatomic,strong) UICollectionView *rightCollectionView ;

@end

@implementation ABSDiscoverViewController

-(NSMutableArray *)dataCollectionData {
    if (!_dataCollectionData) {
        _dataCollectionData = [NSMutableArray array];
    }
    return _dataCollectionData;
}

/** 1:创建数据源*/
-(NSMutableArray *)dataSource {
    if (!_dataSource) {
        
        _dataSource = [NSMutableArray array];
        
//        1: 读取数据
        NSString *bundleFile= [[NSBundle mainBundle] pathForResource:@"text" ofType:@"json"];

        NSData *jsonData = [NSData dataWithContentsOfFile:bundleFile];

        NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

//        NSLog(@"---%@",dict );

        ABSCategoryRootModel *rootModel = [ABSCategoryRootModel mj_objectWithKeyValues:dict];

        NSMutableArray *dataArray = [NSMutableArray array];
        NSMutableArray *dataCollectionArray = [NSMutableArray array];

        for ( int i  = 0 ; i< rootModel.data.count; i++) {

            ABSCategoryFirstModel *firstModel = [ABSCategoryFirstModel mj_objectWithKeyValues:rootModel.data[i]];

            if ( i == 0) {
                firstModel.isSelected = YES ;
                [dataCollectionArray addObject:firstModel];
            }


            [dataArray addObject:firstModel];
        }

        self.dataSource = dataArray;
        self.dataCollectionData = dataCollectionArray;

        
    }
    return _dataSource;
    
}
/** 2: 创建左侧的tableView*/
-(UITableView *)leftTableView {
    if (!_leftTableView) {
        _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 110, ScreenHeight) style:UITableViewStylePlain];
        _leftTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _leftTableView.dataSource = self ;
        _leftTableView.delegate = self ;
        _leftTableView.showsVerticalScrollIndicator = NO;
        
    }
    return _leftTableView;
}


//2: 创建collectivVIew
-(UICollectionView *)rightCollectionView {
    if (!_rightCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _rightCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(120, 0, ScreenWidth - 120, self.view.frame.size.height ) collectionViewLayout:flowLayout];
        
        _rightCollectionView.delegate = self ;
        _rightCollectionView.dataSource = self ;
        
        _rightCollectionView.backgroundColor = [UIColor whiteColor];
        
        _rightCollectionView.alwaysBounceVertical = YES ;
        _rightCollectionView.showsVerticalScrollIndicator = NO ;
        _rightCollectionView.showsHorizontalScrollIndicator = NO ;
        
        _rightCollectionView.translatesAutoresizingMaskIntoConstraints = NO ;
        [_rightCollectionView registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellId];
        
                [ _rightCollectionView registerClass:[ABSCategoryHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIndefine];

    }
    
    return _rightCollectionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

   
    /** UI部分*/
    [self makeUpUIShow];
    
    /** 2: 网络数据*/
//    [self makeUpRequestData];
}

-(void) makeUpUIShow {
     [self.view addSubview:self.rightCollectionView];
    [self.view addSubview:self.leftTableView];
}

-(void)makeUpRequestData{
    
    [ABSToastHUD showToast:@"haha"];
    
    NSDictionary *dict = [NSDictionary dictionary];
       [HttpsBaseRequestTool baseGetRequestWithUrl:CategoryHome parentDict:dict successBlock:^(id  _Nonnull baseSuccessBlock) {
           
            ABSCategoryRootModel *rootModel = [ABSCategoryRootModel mj_objectWithKeyValues:baseSuccessBlock];
           
            NSMutableArray *dataArray = [NSMutableArray array];
            NSMutableArray *dataCollectionArray = [NSMutableArray array];
            
            for ( int i  = 0 ; i< rootModel.data.count; i++) {
                ABSCategoryFirstModel *firstModel = [ABSCategoryFirstModel mj_objectWithKeyValues:rootModel.data[i]];
                if ( i == 0) {
                    [dataCollectionArray addObject:firstModel];
                }
                [dataArray addObject:firstModel];
            }
            
            self.dataSource = dataArray;
            self.dataCollectionData = dataCollectionArray;

           [self.leftTableView reloadData];
           [self.rightCollectionView  reloadData];
       
           [ABSToastHUD dismiss];
       
       } errorBlock:^(id  _Nonnull baseErrorBlock) {
           NSLog(@"---%@",baseErrorBlock);
           
           [ABSToastHUD showToast:@"数据请求失败" afterDelayTime:3];
       }];


}


#pragma mark-tableView 的使用信息
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1 ;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CategoryListLeftCell *cell = [CategoryListLeftCell cellWithTableView:tableView];
    
    ABSCategoryFirstModel *firstModel = self.dataSource[indexPath.row];

    cell.model = firstModel;
    return cell ;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    for (ABSCategoryFirstModel *model in self.dataSource) {
        model.isSelected = NO;
    }

    ABSCategoryFirstModel *firstModel = self.dataSource[indexPath.row];
    /// 不存在复选，只能选择，因此需要首先处理
    firstModel.isSelected = YES ;
    
    [self.leftTableView reloadData];
    
    [self.dataCollectionData removeAllObjects];
    
    [self.dataCollectionData addObject:firstModel];
    
    [self.rightCollectionView reloadData];
    
}



#pragma mark-collectionView 的信息
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        ABSCategoryFirstModel *firstModel = [self.dataCollectionData firstObject] ;
        return  firstModel.children.count;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    ABSCategoryFirstModel *firModel = [self.dataCollectionData firstObject];

    ABSCategorySecondModel *secondModel = firModel.children[section];
    
    NSLog(@"second---%@",secondModel.categoryName);
    return  secondModel.children.count;

}
//4:titem 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return  UIEdgeInsetsMake(10, 10, 10, 10);

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return  CGSizeMake(72, 80);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CategoryCollectionViewCell *collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellId forIndexPath:indexPath];
    ABSCategoryFirstModel *firModel = [self.dataCollectionData firstObject];

    ABSCategorySecondModel *secondModel = firModel.children[indexPath.section];
    
    ABSCategoryThirdModel *thirdModel = secondModel.children[indexPath.row];
    
    
    collectionCell.backgroundColor = [UIColor redColor];
    collectionCell.model = thirdModel;
    return collectionCell ;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld",indexPath.row);
    
    [self.navigationController pushViewController:[[ABSCategoryListController alloc]init] animated:YES];
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return  CGSizeMake(ScreenWidth - 120, 44);
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    ABSCategoryFirstModel *firModel = [self.dataCollectionData firstObject];

    ABSCategorySecondModel *secondModel = firModel.children[indexPath.section];
    
   ABSCategoryHeaderReusableView *header =  [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIndefine forIndexPath:indexPath];
    header.model = secondModel ;
    return header;
   
}
@end
